package com.gy.cloud.cloudb;

import brave.sampler.Sampler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@EnableEurekaClient
@SpringBootApplication
public class CloudBApplication {

    public static void main(String[] args) {
        SpringApplication.run(CloudBApplication.class, args);
        System.out.println("=== 服务B启动成功 === ");
    }

    @Value("${server.port}")
    private String port;

    @GetMapping("/hi")
    public String home(String name) {
        name = name == null ? "SERVICE-B" : name;
        return "Hi " + name + " , I am from port: " + port;
    }

    @Bean
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/info")
    public String info(){
        System.out.println("=== SERVICE-B/info 调取 SERVICE-C/hi ===");
        return restTemplate.getForObject("http://localhost:8763/hi", String.class);
    }

    // 服务追踪分析取样器
    @Bean
    public Sampler defaultSampler() {
        return Sampler.ALWAYS_SAMPLE;
    }

}
